<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form ref="userForm" label-width="100px" :data="userData">
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input
              v-model="userData.departmentName"
              @focus="showTree = true"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 显示可选的部门 -->
      <el-row v-show="showTree" class="tree-box">
        <el-tree
          :data="treeData"
          :props="{ label: 'name' }"
          @node-click="selectNode"
        />
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userData.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- <el-image :src="require('@/assets/common/head.jpg')"></el-image> -->
            <UploadPhoto :user-data.sync="userData.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="xsaveUserDetailById">
            保存更新
          </el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
// 导入格式化树形数据的方案
import { tranListToTreeData } from '@/utils/index.js'
// 导入获取部门列表
import { getDepartments } from '@/api/departments.js'
// 导入修改用户信息
import { saveUserDetailById } from '@/api/employees.js'

export default {
  props: {
    userData: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      // 部门的显示和隐藏
      showTree: false,
      // 部门列表
      treeData: []
    }
  },
  created () {
    this.xgetDepartments()
  },
  methods: {
    // 树形结构转换
    async xgetDepartments () {
      const data = await getDepartments()
      console.log('321231321', data)
      // depts是数组 但不是树形, 需要转换成属性结构, 才能被 el-tree 展示
      this.treeData = tranListToTreeData(data.depts)
    },
    // 点击选中部门
    selectNode (nodedata) {
      console.log(nodedata)
      this.userData.departmentName = nodedata.name
      // 隐藏部门
      this.showTree = false
    },
    // 确认修改用户信息
    async xsaveUserDetailById () {
      // this.$refs.userForm.validate(async flag => {
      // if (!flag) return
      // 调用接口
      await saveUserDetailById(this.userData)
      // 提示成功
      this.$message.success('更新成功!')
      this.$router.back()
      // })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
